package icu.zhangzihan.gis251.controller;

import icu.zhangzihan.gis251.aop.RequiredLog;
import icu.zhangzihan.gis251.mapper.UserMapper;
import icu.zhangzihan.gis251.pojo.dto.UserLoginDTO;
import icu.zhangzihan.gis251.pojo.dto.UserRegDTO;
import icu.zhangzihan.gis251.pojo.dto.UserUpdateDTO;
import icu.zhangzihan.gis251.pojo.entity.User;
import icu.zhangzihan.gis251.pojo.vo.UserVO;
import icu.zhangzihan.gis251.response.JsonResult;
import icu.zhangzihan.gis251.response.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;

@Api(tags = "用户管理模块")
@RestController
@Slf4j
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    UserMapper userMapper;
    //请求头 请求路径  请求体(请求内容)
    //响应头 状态码 响应内容---json
    //     @PathVariable===请求路径
    //  post---请求体(请求内容)---json{name:"yiwei"}--@RequestBody
    //  post---请求体(请求内容)---form  --- 正常接受

    //http://localhost:8080/v1/users/reg
    @ApiOperation(value = "注册功能")
    @PostMapping("/reg")
    //json  字符串格式
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO){
        UserVO userVO = userMapper.selectUserByUserName(userRegDTO.getUserName());
        if (userVO!=null){
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        //插入新用户
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreateTime(new Date());
        // int insertUser(User user);
        userMapper.insertUser(user);
        return JsonResult.ok();
    }

    @ApiOperation(value = "登陆功能")
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, HttpSession session){
        UserVO userVO = userMapper.selectUserByUserName(userLoginDTO.getUserName());
        /*
        userVO  存不存在
        1、存在 判断密码对不对  ①密码对不对  不对  响应密码不正确   ②密码正确  用户登录成功
        2、不存在 --响应用户不存在
         */
        if (userVO!=null){
            if (userVO.getPassword().equals(userLoginDTO.getPassword())){
                session.setAttribute("user",userVO);
                return JsonResult.ok(userVO);
            }
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        }
        return new JsonResult(StatusCode.USERNAME_ERROR);
    }

    //http://localhost:8080/v1/users/logout
    @ApiOperation(value = "登出功能")
    @GetMapping("/logout")
    public JsonResult logout(HttpSession session){
        session.removeAttribute("user");
        return JsonResult.ok(null);
    }

    @ApiOperation(value = "编辑用户信息")
    //http://localhost:8080/v1/users/update
    @PostMapping("/update")
    public JsonResult update(@RequestBody UserUpdateDTO userUpdateDTO){
        //判断---如果更换图像--需要删除原有的图片资源
        if (userUpdateDTO.getImgUrl()!=null){
            //获取数据库原有的图片路径
            String url = userMapper.selectImgUrlById(userUpdateDTO.getId());
            //删除对应的图片文件
//            new File("/var/baking/files"+url).delete();
            new File("D:/xafile"+url).delete();
        }
        //修改用户信息
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        userMapper.updateUserById(user);
        return JsonResult.ok();
    }

    //http://localhost:8080/v1/users/
    @ApiOperation(value = "查询所有用户信息")
    @GetMapping("")
//    <List<UserAdminVO>>
    public JsonResult list(){
        return JsonResult.ok(userMapper.selectAllUser());
    }

    //http://localhost:8080/v1/users/5/0/change
    @ApiOperation(value = "切换管理员")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "long",example = "1"),
            @ApiImplicitParam(name = "isAdmin",value = "是否为管理员",required = true,dataType = "int",example = "1")
    }
    )

    @RequiredLog(value = "修改管理员信息")
    @PostMapping("{id}/{isAdmin}/change")
    public JsonResult change(@PathVariable Long id,
                             @PathVariable Integer isAdmin){
        //调用修改user
        User user=new User();
        user.setId(id);
        user.setIsAdmin(isAdmin);
        userMapper.updateUserById(user);
        return JsonResult.ok();
    }

    //http://localhost:8080/v1/users/5/delete
    @ApiOperation(value = "删除用户")
    @PostMapping("/{id}/delete")
    public JsonResult delete(@PathVariable Long id){
        userMapper.deleteUserById(id);
        return JsonResult.ok();
    }
}
